<?php
namespace Home\Controller;
use Think\Controller;
class UsersController extends Controller {
   public function checkLogin(){
        $usermodel=M('users'); //实例化diuba_users表
        $map   =   array();
        $map['telephone'] =trim(I('post.telephone'));//获取用户输入的手机号
        $map['password']  =md5(trim(I('post.password')));//获取用户输入的密码，并用md5加密
       
        $res=$usermodel->where($map)->select();
        if($res==null){
            $this->error('账号或密码错误');
        }else{
        $_SESSION['telephone']=$map['telephone'];
        $_SESSION['alias']=$res[0]["aliasname"];
        redirect(U("Users/userInfo"));
        }
        }

   public    function check_verify($code, $id = ''){
    $verify = new \Think\Verify();
    return $verify->check($code, $id);
}

    
      public function MyUpload(){
          $upload = new \Think\Upload();// 实例化上传类
          $upload->maxSize   =     3145728 ;// 设置附件上传大小
          $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
          $upload->rootPath  =     './Public/Uploads/headphoto/'; // 设置附件上传根目录  //  $upload->autoSub = false;//关闭子目录，默认true
          $upload->autoSub = false;
         /* $upload->savePath  =     ''; */// 设置附件上传(子)目录，默认目录名为年月日
                  $info   =   $upload->upload();  // 上传文件 
              if(!$info) {     
                //上传错误提示错误信息
                 $this->error($upload->getError());   
                  }
              else{        
              return $info;
                 }                   
                 }

   public function register()
    {
     
      $user = D('Users');
         if($user->create()){     // create 方法
          if($_FILES['file']['error']==0){
            $logoinfo = $this->MyUpload();
            $pname=$logoinfo['file']['savename'];
          }else{
            $pname = "default.jpg";
          }
          $user->photo=$pname;
       if(false!==$user->add()){
             

          $this->success('注册成功!','login');
         }
          else{
            $this->error('数据错误');

          } 
           }
          else {            
              $this->error($user->getError());  
                 }  
  

        } 


     public function doUpdate()
{

    $id=$_POST['id'];
    $userModel=M('users');
    if($_FILES['file']['error']==0)
    {
      $logoinfo=$this->MyUpload();
      $pname=$logoinfo['file']['savename'];
    }
    else
    {
      $pname="default.jpg";
    }
    $_POST['photo']=$pname;
    $res=$userModel-> where("id={$id}") -> data($_POST)->save();
    if($res)
    {
      $this->success('修改成功,请重新登录','login');
      //$this->logout();
    }
    else{
      $this->error('修改失败');
    }
  }
public function edituser(){

  $user = M('users');
  $data['telephone']=$_SESSION['telephone'];
  $res = $user->where($data)->find();
  $this->userInfos=$res;
  $this->display();
}

public function verify(){
    ob_clean();//清空缓存
    $Verify = new \Think\Verify();//调用验证码类实例化对象
    $Verify->entry();//输出验证码图片，并将验证码加密处理后放到Session中
  }
public  function userInfo(){
        $userInfo = M('users');
        $data['telephone']=$_SESSION['telephone'];
        $userInfo = $userInfo->where($data)->find();
        $this->userInfo=$userInfo;

        $lost = M('sight');
        $lostData['telephone'] = $data['telephone'];
       /* $qrcodenum = $lost->where($lostData)->count();*/
       /* $Page = new \Think\Page($qrcodenum, 2);*/
        $this->lostList = $lost->where($lostData)->order("id desc")->select();


        $find = M('search');
        $findData['telephone'] = $data['telephone'];
       /* $qrcodenum = $lost->where($findData)->count();*/
        /*$Page = new \Think\Page($qrcodenum, 2);*/
        $this->findList = $find->where($findData)->order("id desc")->select();

   $this->display();

}
   public function checkTele(){
      $t=I('get.t');
      $userModel = M("Users");
      $rs=$userModel->where("telephone='$t'")->select();
    /*  $this->ajaxReturn($rs);*/
      if($rs) echo "手机号已存在";

     }
      public function checkT(){
      $t=I('get.t');
      $userModel = M("Users");
      $rs=$userModel->where("aliasname='$t'")->select();
      if($rs) echo "用户名已存在";

      
      
      
     }
      public function checkAlias(){
      $u=$_GET["u"];
      $userModel = M("Users");
      $rs=$userModel->field('aliasname')->where("aliasname like '%$u%'")->limit(4)->select();
      //echo $userModel->getLastSQL();
      $this->ajaxReturn($rs);
     }

}